﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using Domen;
using System.Collections.Generic;
using System.Web;

namespace Sesija
{
    public class BrokerLFS
    {
        string url = "";

        private static BrokerLFS instance;
        public static BrokerLFS GetInstance()
        {
            if (instance == null)
            {
                instance = new BrokerLFS();
            }
            return instance;
        }

        private StreamReader ConnetingToLFS()
        {
            WebRequest webReq = WebRequest.Create(url);
            WebResponse webRes = webReq.GetResponse();
            Stream stream = webRes.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            return reader;
        }


        public List<DriverStats> ReturnStatisticsPST(Driver vozac) //Vraca osnovnu statistiku vozaca
        {

            List<DriverStats> vozacL = new List<DriverStats>();

            //vozac.IDK = "G7e9BxIprDBZloM1576G9em4lPFT6GcW";

            url = "http://www.lfsworld.net/pubstat/get_stat2.php?version=1.5&idk=" + vozac.IDK + "&racer=" + vozac.Username + "&action=pst";

            List<string> pomocnaLista = new List<string>();
            StreamReader reader = ConnetingToLFS();
            while (!reader.EndOfStream)
            {
                pomocnaLista.Add(reader.ReadLine());
            }


            DriverStats vozacStat = new DriverStats();
            //readLine = reader.ReadToEnd();
            //string[] statistika = readLine.Split();
            try
            {
                vozacStat.TravelledDistance = Convert.ToInt32(pomocnaLista[0]);
                vozacStat.FuelBurnt = Convert.ToInt32(pomocnaLista[1]);
                vozacStat.Laps = Convert.ToInt32(pomocnaLista[2]);
                vozacStat.HostJoined = Convert.ToInt32(pomocnaLista[3]);
                vozacStat.RaceWon = Convert.ToInt32(pomocnaLista[4]);
                vozacStat.Second = Convert.ToInt32(pomocnaLista[5]);
                vozacStat.Third = Convert.ToInt32(pomocnaLista[6]);
                vozacStat.Finished = Convert.ToInt32(pomocnaLista[7]);
                vozacStat.Qualifications = Convert.ToInt32(pomocnaLista[8]);
                vozacStat.PolePosition = Convert.ToInt32(pomocnaLista[9]);
                vozacStat.Drags = Convert.ToInt32(pomocnaLista[10]);
                vozacStat.DragsWins = Convert.ToInt32(pomocnaLista[11]);
                vozacStat.Country = pomocnaLista[12];
                vozacStat.OnlineStatus = Convert.ToInt32(pomocnaLista[13]);
                vozacStat.Host = pomocnaLista[14];
                vozacStat.LastActivityTime = Convert.ToInt32(pomocnaLista[15]);
                vozacStat.Track = Convert.ToInt32(pomocnaLista[16]);
                vozacStat.Car = pomocnaLista[17];
            }
            catch (Exception ex)
            {
                vozacL.Add(vozacStat);

                return vozacL;
            }
            vozacL.Add(vozacStat);
            return vozacL;
        }

        public Bitmap CreateImage(Driver vozac, Bitmap image)
        {
            List<DriverStats> driverStatsList = new List<DriverStats>();
            driverStatsList = ReturnStatisticsPST(vozac);

            DriverStats driverStat = new DriverStats();

            driverStat.TravelledDistance = driverStatsList[0].TravelledDistance;
            driverStat.RaceWon = driverStatsList[0].RaceWon;
            driverStat.Country = driverStatsList[0].Country;
            driverStat.OnlineStatus = driverStatsList[0].OnlineStatus;

            string onlineStatus = "";
            switch (driverStat.OnlineStatus)
            {
                case 0:
                    onlineStatus = "offline";
                    break;
                case 1:
                    onlineStatus = "spectating";
                    break;
                case 2:
                    onlineStatus = "pits";
                    break;
                case 3:
                    onlineStatus = "in-race";
                    break;
                default:
                    onlineStatus = "unknown";
                    break;
            }

            Graphics graphicImage = Graphics.FromImage(image);

            graphicImage.SmoothingMode = SmoothingMode.AntiAlias;

            //graphicImage.DrawArc(new Pen(Color.Red, 3), 1, 1, 300, 30, 0, 360);
            graphicImage.DrawString(vozac.Username.ToUpper() + "  --->  " + onlineStatus + "\n" + "Total: " + (int)driverStat.TravelledDistance / 1000 + "km     Race won: " + driverStat.RaceWon + "     Country: " + driverStat.Country,
                new Font("Arial", 8, FontStyle.Bold), new SolidBrush(Color.White), new Point(4, 2));

            graphicImage.Dispose();

            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Gif);
            return image;
        }
    }
}
